﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 文件名：ClsSellFundManageDAL.cs
/// 作  者：黄纲
/// 时  间：2007.01.11
/// 说  明：ClsSellFundManageDAL类用于销售收退款管理信息相关处理
/// </summary>
public class ClsSellFundManageDAL
{
    #region  数据库连接字符串
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    #endregion

    /// <summary>
    /// 获取销售收退款主单信息
    /// </summary>
    /// <param name="F_Type">类别</param>
    /// <param name="UserID">当前登陆用户ID</param>
    /// <param name="Type">返回数据集类型</param>
    /// <returns>返回数据集</returns>
    public DataSet GetSellFundDataSet(int F_Type, string UserID, int Type)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetSellFundDataSet", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@f_type", SqlDbType.Int, 4);
        dat.SelectCommand.Parameters["@f_type"].Value = F_Type;
        dat.SelectCommand.Parameters.Add("@u_log_id", SqlDbType.NVarChar, 60);
        dat.SelectCommand.Parameters["@u_log_id"].Value = UserID;
        dat.SelectCommand.Parameters.Add("@type", SqlDbType.Int, 4);
        dat.SelectCommand.Parameters["@type"].Value = Type;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 获取所有销售收退款从表信息
    /// <param name="Type"></param>
    /// <param name="No">应收单号</param>
    /// </summary>
    /// <returns>返回数据集</returns>
    public DataSet GetSellFundChildDataSet(int Type, string No)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetSellFundChildDataSet", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@f_type", SqlDbType.Int, 4);
        dat.SelectCommand.Parameters["@f_type"].Value = Type;
        dat.SelectCommand.Parameters.Add("@f_no", SqlDbType.NVarChar, 50);
        dat.SelectCommand.Parameters["@f_no"].Value = No;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 新增或修销售收退款主单信息
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="Type"></param>
    /// <param name="No"></param>
    /// <param name="Date"></param>
    /// <param name="Cus_No"></param>
    /// <param name="Dp_No"></param>
    /// <param name="Wk_No"></param>
    /// <param name="Oper"></param>
    /// <param name="Remark"></param>
    /// <returns></returns>
    public Boolean AddOrUpdateInfo(int ID, int Type, string No, DateTime Date, string Cus_No, string Dp_No, string Wk_No, string C_Type, string Oper, string Remark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddOrUpdateSellFundInfo", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@id", SqlDbType.Int, 4);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@f_type", SqlDbType.Int, 4);
        com.Parameters["@f_type"].Value = Type;
        com.Parameters.Add("@f_no", SqlDbType.NVarChar, 50);
        com.Parameters["@f_no"].Value = No;
        com.Parameters.Add("@f_date", SqlDbType.DateTime, 8);
        com.Parameters["@f_date"].Value = Date;
        com.Parameters.Add("@cus_no", SqlDbType.NVarChar, 50);
        com.Parameters["@cus_no"].Value = Cus_No;
        com.Parameters.Add("@dp_id", SqlDbType.NVarChar, 50);
        com.Parameters["@dp_id"].Value = Dp_No;
        com.Parameters.Add("@wk_id", SqlDbType.NVarChar, 50);
        com.Parameters["@wk_id"].Value = Wk_No;
        com.Parameters.Add("@c_type", SqlDbType.NVarChar, 50);
        com.Parameters["@c_type"].Value = C_Type;
        com.Parameters.Add("@oper", SqlDbType.NVarChar, 50);
        com.Parameters["@oper"].Value = Oper;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 新增或修改销售收退款从单信息
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="Type"></param>
    /// <param name="No"></param>
    /// <param name="Up_No"></param>
    /// <param name="Up_Date"></param>
    /// <param name="Im_No"></param>
    /// <param name="Money1"></param>
    /// <param name="Money2"></param>
    /// <param name="Money3"></param>
    /// <param name="Agio"></param>
    /// <param name="A_agio"></param>
    /// <param name="B_agio"></param>
    /// <param name="C_agio"></param>
    /// <param name="Remark"></param>
    /// <param name="Cus_No"></param>
    /// <returns></returns>
    public Boolean AddOrUpdateChild(int ID, int Type, string No, string Up_No, DateTime Up_Date, decimal Money2, decimal Agio, decimal A_agio, decimal B_agio, decimal C_agio, string Remark, string Cus_No, string UserID)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddOrUpdateSellFundChild", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@id", SqlDbType.Int, 4);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@f_type", SqlDbType.Int, 4);
        com.Parameters["@f_type"].Value = Type;
        com.Parameters.Add("@f_no", SqlDbType.NVarChar, 50);
        com.Parameters["@f_no"].Value = No;
        com.Parameters.Add("@up_no", SqlDbType.NVarChar, 50);
        com.Parameters["@up_no"].Value = Up_No;
        com.Parameters.Add("@up_no_date", SqlDbType.DateTime, 8);
        com.Parameters["@up_no_date"].Value = Up_Date;
        com.Parameters.Add("@money2", SqlDbType.Decimal, 28);
        com.Parameters["@money2"].Value = Money2;
        com.Parameters.Add("@p_agio", SqlDbType.Decimal, 28);
        com.Parameters["@p_agio"].Value = Agio;
        com.Parameters.Add("@a_agio", SqlDbType.Decimal, 28);
        com.Parameters["@a_agio"].Value = A_agio;
        com.Parameters.Add("@b_agio", SqlDbType.Decimal, 28);
        com.Parameters["@b_agio"].Value = B_agio;
        com.Parameters.Add("@c_agio", SqlDbType.Decimal, 28);
        com.Parameters["@c_agio"].Value = C_agio;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;
        com.Parameters.Add("@cus_no", SqlDbType.NVarChar, 50);
        com.Parameters["@cus_no"].Value = Cus_No;
        com.Parameters.Add("@u_log_id", SqlDbType.NVarChar, 50);
        com.Parameters["@u_log_id"].Value = UserID;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 作废销售收退款单
    /// </summary>
    /// <param name="F_type">0收款，1退款</param>
    /// <param name="ID">应收单ID</param>
    /// <param name="Type">0作废，1彻底作废</param>
    /// <returns></returns>
    public Boolean DeleteInfo(int F_type, int ID, int Type)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteSellFundInfo", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@f_type", SqlDbType.Int, 4);
        com.Parameters["@f_type"].Value = F_type;
        com.Parameters.Add("@id", SqlDbType.Int, 4);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@type", SqlDbType.Int, 4);
        com.Parameters["@type"].Value = Type;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 作废销售收退款从单
    /// </summary>
    /// <param name="F_type">0收款，1退款</param>
    /// <param name="ID">应收从单ID</param>
    /// <param name="Type">0作废，1彻底作废</param>
    /// <returns></returns>
    public Boolean DeleteChildInfo(int F_type, string Up_No, int ID, int Type)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteSellFundChildInfo", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@f_type", SqlDbType.Int, 4);
        com.Parameters["@f_type"].Value = F_type;
        com.Parameters.Add("@up_no", SqlDbType.NVarChar, 50);
        com.Parameters["@up_no"].Value = Up_No;
        com.Parameters.Add("@id", SqlDbType.Int, 4);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@type", SqlDbType.Int, 4);
        com.Parameters["@type"].Value = Type;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 确认或取消确认销售收退款
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="UserID"></param>
    /// <param name="Type"></param>
    /// <returns></returns>
    public Boolean AuditOrOAuditSellFundInfo(int ID, string UserID, int Type)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AuditOrOAuditSellFundInfo", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@id", SqlDbType.Int, 4);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@user", SqlDbType.NVarChar, 50);
        com.Parameters["@user"].Value = UserID;
        com.Parameters.Add("@type", SqlDbType.Int, 4);
        com.Parameters["@type"].Value = Type;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 获取当前客户的应收和发票信息
    /// </summary>
    /// <param name="Param">查询参数</param>
    /// <param name="Mode">查询方式(0为精确,1为模糊)</param>
    /// <param name="CurrCustomer">客户编号</param>
    /// <param name="CurrUser">当前登陆用户ID</param>
    /// <returns></returns>
    public DataSet GetCurrCustomerSellFundDataSet(string Param, int Mode, string CurrCustomer, string CurrUser)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetCurrCustomerSellFundDataSet", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@param", SqlDbType.NVarChar, 60);
        dat.SelectCommand.Parameters["@param"].Value = Param;
        dat.SelectCommand.Parameters.Add("@mode", SqlDbType.Int, 4);
        dat.SelectCommand.Parameters["@mode"].Value = Mode;
        dat.SelectCommand.Parameters.Add("@cus_no", SqlDbType.NVarChar, 50);
        dat.SelectCommand.Parameters["@cus_no"].Value = CurrCustomer;
        dat.SelectCommand.Parameters.Add("@u_log_id", SqlDbType.NVarChar, 60);
        dat.SelectCommand.Parameters["@u_log_id"].Value = CurrUser;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }
}
